﻿var _ngUnvsWebServiceCallBack=angular.module("ng-unvs-webservice-callback", []);
_ngUnvsWebServiceCallBack.service("$wsConfig", ["$http", function ($http) {
    return {
        $setUrl: function (url) {
            if (!window.top.ngW8["ng-unvs-webservice-callback"]) {
                window.top.ngW8["ng-unvs-webservice-callback"] = {};
            }
            window.top.ngW8["ng-unvs-webservice-callback"].url = url;
        },
        $getUrl:function(){
            return  window.top.ngW8["ng-unvs-webservice-callback"].url;

        },
        $call: function (controller, data, success, error) {
            var url = window.top.ngW8["ng-unvs-webservice-callback"].url +"/"+ controller;
            $http.post(url, data).then(function (res) {
                if (success) {
                    success(res);
                }
            }, function (error) {
                if (error) {
                    error(error);
                }
               
            });
        }
    }
}]);
_ngUnvsWebServiceCallBack.service("$ws", ["$http", "$wsConfig", function ($http, $wsConfig) {
    function _$ws(controller) {
        try {
            this.controller = controller;
            this.rootUrl = $wsConfig.$getUrl();
            var me=this;
            this.param = function (data) {
                me.$data = data;
                return me;
            }
            this.success = function (callback) {
                me.successCallBack = callback;
                return me;
            };
            this.error = function (callback) {
                me.errorCallBack = callback;
                return me;
            };
            this.post = function () {
                $http.post(me.rootUrl + "/" + me.controller, me.$data).success(function (res) {
                    if (me.successCallBack) {
                        me.successCallBack(res);
                    }
                }).then(function (res) { }, me.errorCallBack);
                return me;
            };
            this.get = function () {
                $http.get(me.rootUrl + "/" + me.controller).success(function (res) {
                    if (me.successCallBack) {
                        me.successCallBack(res);
                    }
                }).then(function (res) { }, me.errorCallBack);
                return me;
            };
            return this;
        } catch (e) {
            throw (e);
        }
    };
    return _$ws;
}]);